﻿@inherits NodesBase

@if (UI != null && CollectionsVisible != null)
{
    <ul class="nav flex-column">
        @if (PageNr > 1)
        {
            <li class="nav-item">
                <button @onclick="@((MouseEventArgs e) => OnPageChangeAsync(-1))" class="paginate" title="Previous items" disabled="@Loading"><i class="icon ion-md-arrow-dropup-circle"></i></button>
            </li>
        }
        @foreach (var node in UI.Nodes)
        {
            <li class="nav-item">
                <button @onclick="@((MouseEventArgs e) => CollectionsVisible[node.Id] = !CollectionsVisible[node.Id])" class="toggle">
                    @if (CollectionsVisible[node.Id])
                    {
                        <i class="icon ion-md-arrow-dropdown"></i>
                    }
                    else
                    {
                        <i class="icon ion-md-arrow-dropright"></i>
                    }
                </button>


                @if (node.State != null)
                {
                    <NavigationLink NewState="@node.State">@node.Name</NavigationLink>
                }
                else
                {
                    <a href="/unauthorized" class="disabled">@node.Name</a>
                }

                <ul class="nav flex-column">
                    @if (CollectionsVisible[node.Id])
                    {
                        @foreach (var (subCollection, _) in node.Collections)
                        {
                            <Collections CollectionAlias=@subCollection ParentPath=@(ParentPath.AddLevel(ParentPath, node.RepositoryAlias, node.Id)) />
                        }
                    }
                </ul>
            </li>
        }
        @if (UI.MoreDataAvailable)
        {
            <li class="nav-item">
                <button @onclick="@((MouseEventArgs e) => OnPageChangeAsync(1))" class="paginate" title="Next items" disabled="@Loading"><i class="icon ion-md-arrow-dropdown-circle"></i></button>
            </li>
        }
    </ul>
}
else if (!string.IsNullOrWhiteSpace(error))
{
    <ul class="nav flex-column">
        <li>@error</li>
    </ul>
}

@code {

    Dictionary<string, bool>? CollectionsVisible;
    TreeNodesUI? UI;
    string? error;

    int PageSize = 25;
    int PageNr = 1;
    bool Loading = false;

    protected override async Task OnParametersSetAsync()
    {
        await OnNodesUpdateAsync();
    }

    private async Task OnPageChangeAsync(int delta)
    {
        Loading = true;
        PageNr += delta;
        await OnNodesUpdateAsync();
        Loading = false;
    }

    protected override async Task OnNodesUpdateAsync()
    {
        var oldNodesVisible = CollectionsVisible;

        try
        {
            UI = await TreeService.GetNodesAsync(CollectionAlias, ParentPath, PageNr, PageSize);
            if (UI == null)
            {
                return;
            }

            CollectionsVisible = UI.Nodes.ToDictionary(x => x.Id, x => x.DefaultOpenCollections);

            // restore the view state with the new nodes
            if (oldNodesVisible != null)
            {
                foreach (var node in oldNodesVisible.Where(x => x.Value))
                {
                    if (CollectionsVisible.ContainsKey(node.Key))
                    {
                        CollectionsVisible[node.Key] = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            error = ex.Message;
        }

        StateHasChanged();
    }
}
